var Columns = Ext.extend(Ext.Panel, {
	border : false,
	layout : 'border',
	initComponent : function() {
		this.initTables();
		this.initColumns();
		this.items = [this.tables, this.columns];
		Columns.superclass.initComponent.call(this);
	},
	initTables : function() {
		var me = this;
		this.tables = new Ext.grid.GridPanel({
			region : 'west',
			width : 300,
			split : true,
			loadMask : true,
			viewConfig : {
				forceFit : true
			},
			bodyStyle : 'border-left:0;border-bottom:0;border-top:0;',
			store : new Ext.data.JsonStore({
						fields : ['tablename', 'chinesename'],
						url : My.url('/system/common/queryForList.do'),
						baseParams : {
							table : 't_tables',
							where : 'order by tablename',
							cols : '*'
						},
						root : 'data',
						autoLoad : true
					}),
			sm : new Ext.grid.RowSelectionModel({
				listeners : {
					rowselect : function(t, i, r) {
						// 加载菜单
						var rec = me.tables.getStore().getAt(i);
						me.columns.getStore().baseParams.where = "where upper(tablename)=upper('"
								+ rec.data.tablename + "') order by columnname";
						me.columns.getStore().load();
					}
				}
			}),
			cm : new Ext.grid.ColumnModel({
						columns : [{
									dataIndex : 'tablename',
									header : '表名',
									width : 300,
									renderer : function(v, m, r) {
										return v + '（' + r.data.chinesename
												+ '）';
									}
								}]
					})
		});
	},
	initColumns : function() {
		var me = this;
		this.columns = new Ext.grid.GridPanel({
			region : 'center',
			loadMask : true,
			bodyStyle : 'border-right:0;border-bottom:0;',
			store : new Ext.data.JsonStore({
						fields : ['tablename', 'columnname', 'chinesename'],
						url : My.url('/system/common/queryForList.do'),
						baseParams : {
							table : 't_columns',
							where : 'order by columnname',
							cols : '*'
						},
						root : 'data'
					}),
			viewConfig : {
				forceFit : true
			},
			sm : new Ext.grid.RowSelectionModel(),
			cm : new Ext.grid.ColumnModel({
						columns : [{
									dataIndex : 'columnname',
									header : '字段名',
									width : 300
								}, {
									dataIndex : 'chinesename',
									header : '中文名',
									width : 300
								}]
					}),
			tbar : new Ext.Toolbar({
				style : 'border-top:0;border-rigth:0;',
				items : [{
					text : '同步',
					iconCls : 'icon-sync',
					handler : function() {
						My.request({
							url : '/system/common/exec.do',
							params : {
								sql : "begin\n"
										+ "  insert into t_columns\n"
										+ "    (tablename, columnname)\n"
										+ "    select table_name, column_name\n"
										+ "      from (select a.table_name, a.column_name\n"
										+ "              from user_tab_columns a\n"
										+ "              join t_tables b\n"
										+ "                on a.table_name = upper(b.tablename))\n"
										+ "    minus\n"
										+ "    select tablename, columnname from t_columns;\n"
										+ "  delete t_columns\n"
										+ "   where tablename || '.' || columnname in\n"
										+ "         (select tablename || '.' || columnname\n"
										+ "            from t_columns\n"
										+ "          minus\n"
										+ "          select table_name || '.' || column_name\n"
										+ "            from (select a.table_name, a.column_name\n"
										+ "                    from user_tab_columns a\n"
										+ "                    join t_tables b\n"
										+ "                      on a.table_name = upper(b.tablename)));\n"
										+ "end;"

							},
							successCallback : function() {
							}
						});
					}
				}, {
					text : '修改',
					iconCls : 'icon-update',
					handler : function() {
						me.update();
					}
				}]
			})
		});
	},
	update : function() {
		var me = this;
		var rec = this.columns.getSelectionModel().getSelected();
		if (!rec) {
			TopMy.alert('请选择要修改的记录！');
			return;
		}
		var form = new TopExt.FormPanel({
					border : false,
					padding : 5,
					defaults : {
						xtype : 'textfield',
						anchor : '100%'
					},
					labelAlign : 'right',
					labelWidth : 70,
					items : [{
								name : 'tablename',
								fieldLabel : '表&emsp;&emsp;名',
								readOnly : true
							}, {
								name : 'columnname',
								fieldLabel : '字&ensp;段&ensp;名',
								readOnly : true
							}, {
								name : 'chinesename',
								fieldLabel : '中&ensp;文&ensp;名',
								allowBlank : false
							}],
					listeners : {
						afterrender : function(t) {
							t.getForm().loadRecord(rec);
						}
					}
				});
		var win = new TopExt.Window({
			title : '修改表信息',
			width : 380,
			autoHeight : true,
			items : [form],
			modal : true,
			buttons : [{
				text : '保存',
				iconCls : 'icon-save',
				handler : function() {
					TopMy.submit(form, {
						url : My.url('/system/common/exec.do'),
						params : {
							sql : "update t_columns set chinesename=#{chinesename} where tablename=#{tablename} and columnname=#{columnname}"
						},
						successCallback : function() {
							me.columns.getStore().reload();
							win.close();
						}
					});
				}
			}, {
				text : '取消',
				iconCls : 'icon-cancel',
				handler : function() {
					win.close();
				}
			}]
		});
		win.show();
	}
});
Ext.onReady(function() {
			new Ext.Viewport({
						layout : 'fit',
						items : [new Columns()]
					});
		});